﻿using UnityEngine;
using System.Collections;

public class GameNetworkManager : MonoBehaviour {

    public City city;
    public ClientRecommendationManager clientRecommendationManager;
    public CityManager cityManager;
    public SideMenu sideMenu;
  /// <summary>
  /// Steve- I declared score up here and added the gameScore varialbes so that in the start function of the SideMenu script
  ///the tablet would grab the over all score fromt the server so they would remain in sink
  /// </summary>
    float score;
    public float gameScore
    {
      get
      {
        return score;
      }
      set
      {
        gameScore = value;
      }
    }

    // Use this for initialization
	void Start () {

        if (Network.isServer)
        {
            DontDestroyOnLoad(this.gameObject);
        }

	}
	
	// Update is called once per frame
	void Update () {
	
	}

    void OnNetworkInstantiate(NetworkMessageInfo info)
    {
        DontDestroyOnLoad(this.gameObject);
        if (Network.isClient)
        {
            Application.LoadLevel("gameTablet");
        }
    }

    [RPC]
    public void RequestRecommendation(NetworkMessageInfo info)
    {
        print("RPC RequestRecommendation called");
        if (Network.isServer)
        {
            print("Received request for recommendation on the server");
            Recommendation.RecommendationCode recommendationCode = city.GetRecommendationCode();
            networkView.RPC("OnReceiveRecommendation", info.sender, (int)recommendationCode);
        }
    }

    /// <summary>
    /// Bomani - this method is called on the client/tablet when a 
    /// recommendation has been received from the server. You will
    /// need to designate what content appears where based on that code.
    /// See the Recommendation class for details.
    /// </summary>
    /// <param name="recommendationValue"></param>
    [RPC]
    public void OnReceiveRecommendation(int recommendationValue)
    {
        Recommendation.RecommendationCode recommendationCode = (Recommendation.RecommendationCode)recommendationValue;
        Recommendation recommendation = new Recommendation(recommendationCode);

        // TODO: Make this recommendation appear on the screen through clientRecommendationManager
        sideMenu.recText = recommendationCode.ToString();
        print("Recommendation " + recommendationCode.ToString() + " received");
    }

    public void ClientRequestRecommendation()
    {
        print("Client requested recommendation");
        networkView.RPC("RequestRecommendation", RPCMode.Server);
    }

    void OnSerializeNetworkView(BitStream stream, NetworkMessageInfo info)
    {
        if (stream.isWriting)
        {
            print("Writing score " + cityManager.currentScore + " to clients");
            float score = cityManager.currentScore;
            stream.Serialize(ref score);
        }
        else
        {
            score = 0;
            stream.Serialize(ref score);

            /// Bomani - use the value of the score variable to determine
            /// what to display on the score meter. 

            //passes the score to the SideMenu on the tablet
            if (sideMenu != null) sideMenu.score = score;

            print("Score from server is " + score);
        }
    }
}
